package com.zjuh.qss.biz.bo.impl;

import java.io.File;
import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import wint.core.service.ServiceContext;
import wint.core.service.env.Environment;
import wint.core.service.initial.ServiceContextAwire;
import wint.help.server.http.FileHttpServer;
import wint.lang.WintException;

public class HttpServerBOImpl implements ServiceContextAwire {

	private final static Logger log = LoggerFactory.getLogger(HttpServerBOImpl.class);
	
	private int port;
	
	private String filePath;
	
	private String path = "/";
	
	private ServiceContext serviceContext;
	
	private FileHttpServer fileHttpServer;
	
	@Override
	public void setServiceContext(ServiceContext serviceContext) {
		this.serviceContext = serviceContext;
	}
	
	public void init() {
		if (serviceContext == null) {
			return;
		}
		if (serviceContext.getConfiguration().getEnvironment() == Environment.DEV) {
			try {
				log.info("file http server starting up, port:" + port);
				fileHttpServer = new FileHttpServer(port, path, new File(filePath));
				fileHttpServer.start();
				log.info("file http server has been started up.");
			} catch (IOException e) {
				throw new WintException(e);
			}
		}
	}
	
	public void destroy() {
		fileHttpServer.stop();
	}

	public void setPort(int port) {
		this.port = port;
	}

	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}

	public void setPath(String path) {
		this.path = path;
	}

}
